<script setup lang="ts">
import {Delete, Edit, Plus, InfoFilled} from "@element-plus/icons-vue";
import {useCategoryStore} from "@/store/category.ts";
import {reactive, ref} from 'vue'
import {addCategoryAPI} from "@/api/creation.ts";
import {ElMessage, ElNotification} from "element-plus";

const categoryStore = useCategoryStore()
const dialogVisible = ref(false)
const ruleForm = reactive({
  name: '',
  nickname: '',
  icon: ''
})

// 图片上传成功
const onSuccess = (response) => {
  ruleForm.icon = response.data
}

// 添加分类
const addCategory = async () => {
  const res = await addCategoryAPI(ruleForm)
  if (res.code === 0) {
    ElMessage({
      type: "success",
      message: '添加成功'
    })
    dialogVisible.value = false
  }
  useCategoryStore.getCategoryList()
}

// 删除分类
const deleteCategory = async () => {
  ElNotification({
    type: 'warning',
    title: '失败',
    message: '一级分类暂不支持删除',
    duration: 2000
  })
}

// 编辑分类
const EditCategory = () => {
  ElNotification({
    title: 'Warning',
    message: '为减缓服务器压力，暂关闭编辑接口及数据回显',
    type: 'warning',
  })
}
</script>

<template>
  <el-card class="card-category">
    <div class="card-category-list column">
      <div class="card-category-list-title">
        <el-button type="primary" @click="dialogVisible=true">添加分类</el-button>
      </div>
      <div class="card-category-list-table">
        <el-table
            border
            class="el-table-box"
            :data="categoryStore.categoryList"
            ref="multipleTableRef"
            style="width: 100%"
        >
          <el-table-column type="index" label="#" align="center"/>
          <el-table-column prop="_id" align="center" label="id"/>
          <el-table-column prop="name" align="center" label="名称" width="120"/>
          <el-table-column prop="nickname" align="center" label="昵称" width="120"/>
          <el-table-column align="center" label="图标" width="120">
            <template #default="{row}">
              <img style="width: 40px; height: 40px"
                   :src="row.icon || 'http://img.hangdan.top/cgym/index/%E5%9B%BE%E4%BE%8B.svg'" alt="">
            </template>
          </el-table-column>
          <el-table-column align="center" label="分类" width="200">
            <template #default="{row}">
              <el-tag v-for="item in row?.subCategoryId.slice(0, 6)" :key="item._id" style="margin: 5px">
                {{ item.nickname }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" label="控制" width="180">
            <template #default="{row}">
              <div class="operator-btn">
                <el-button type="primary" size="default" @click="EditCategory" :icon="Edit as any" circle/>
                <el-popconfirm
                    width="200"
                    @confirm="deleteCategory()"
                    confirm-button-text="确定"
                    cancel-button-text="取消"
                    :icon="InfoFilled as any"
                    icon-color="#f36b6c"
                    title="您确定删除此部位吗?"
                >
                  <template #reference>
                    <el-button type="danger" size="default" :icon="Delete as any" circle/>
                  </template>
                </el-popconfirm>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  </el-card>
  <el-dialog v-model="dialogVisible" title="添加分类">
    <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        status-icon
        :rules="rules"
        style="padding: 0 50px"
        class="demo-ruleForm"
    >
      <el-form-item label="名称" prop="name">
        <el-input v-model="ruleForm.name" type="text" autocomplete="off"/>
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="ruleForm.nickname" type="text" autocomplete="off"/>
      </el-form-item>
      <el-form-item label="图标">
        <el-upload action="http://116.62.147.119:3636/api/category/upload"
                   :on-success="onSuccess"
                   list-type="picture-card">
          <el-icon>
            <Plus/>
          </el-icon>
        </el-upload>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="addCategory">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style scoped lang="scss">
.card-category {
  padding: 0 50px;

  .card-category-list {

    .card-category-list-title {

      display: flex;
      justify-content: flex-end;
    }

    .card-category-list-table {
      margin-top: 20px;

      .el-table-box {
        height: calc(90vh - 140px);
      }
    }
  }
}
</style>